package org.pizza.mybatis.plus.support;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.pizza.context.BaseContext;

import java.util.Date;

/**
 * @author 高巍
 * @since 2019年06月27日 14:18
 */

public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createFieldType = getFieldValByName("createTime", metaObject);
        Object updateFieldType = getFieldValByName("updateTime", metaObject);
        if (createFieldType == null) {
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
            this.strictInsertFill(metaObject, "createUser", Long.class, BaseContext.operator());
            this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
            this.strictInsertFill(metaObject, "updateUser", Long.class, BaseContext.operator());
            this.strictInsertFill(metaObject, "tenantId", Long.class, BaseContext.tenant());
        }
        if (updateFieldType == null) {
            this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
            this.strictInsertFill(metaObject, "updateUser", Long.class, BaseContext.operator());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateUser", Long.class, BaseContext.operator());
    }
}
